* Run on Stata 13 for Mac (OS 9)
* June 3, 2016


********************************************
* CODE FOR TABLES A3, A4, A5 AND A6
********************************************

*** Set Path
* cd "..."

use "firm_industry_country.dta", clear

* Winsorize based on Labor
sort digit2
bysort digit2: egen max=pctile(labor), p(99)
bysort digit2: egen min=pctile(labor), p(1) 
bysort digit2: gen winso_labor = labor if (labor>min & labor<max)
drop max min
drop if winso_labor == . 


* Government Ownership
gen government = 1 if public_share>50 & public_share<= 100
replace government = 0 if public_share>=0 & public_share<=50

* Other firm-level controls
gen ln_firm_age = ln(age)
gen ln_firm_labor = ln(labor)
gen ln_tot_pop = ln(tot_pop)
gen ln_firm_exports = ln(1+exportshare)
gen obsolete_firm = 0 if iso == 1
replace obsolete_firm = 1 if iso == 0
gen inspected = . 
replace inspected = 0 if inspections == 0
replace inspected = 1 if inspections > 0 & inspections<1000

* Normalized Tax Compliance
gen scaled_taxcomp = taxcomp/100

* Industry-Level: Number of Competitors
bysort idc digit2: egen avgcompetitors = mean(competitors)
drop if avgcompetitors == .

* Country-Level: Fiscal Capacity
gen lowfc = -1*taxstaff
	

******************************************************************
* TABLE A-3 - MODELING RESPONSES
*****************************************************************

* Non-response
gen response = 0 if taxcomp ==. 
replace response = 1 if taxcomp !=.

* Response without context-controls
probit response ln_firm_age obsolete_firm exportshare ln_firm_labor government avgcompetitors i.digit2  i.region  taxstaff tariff ln_tot_pop, cluster(country)
*outreg2 using ".../response_and_free_media.xls", dec(3) alpha(.01, .05, .1) replace

* Free Media
probit response free_media ln_firm_age obsolete_firm exportshare ln_firm_labor government avgcompetitors  i.digit2  i.region  taxstaff tariff ln_tot_pop, cluster(country)
*outreg2 using ".../response_and_free_media.xls", dec(3) alpha(.01, .05, .1) append

* Polity 
probit response polity ln_firm_age obsolete_firm exportshare ln_firm_labor government avgcompetitors  i.digit2  i.region  taxstaff tariff ln_tot_pop, cluster(country)
*outreg2 using ".../response_and_free_media.xls", dec(3) alpha(.01, .05, .1) append


******************************************************************
* TABLE A-4 -- PERCEPTION BIAS AND WEIGHTING -- OLS MODELS
*****************************************************************

* Non-Response Weighting Factor [Desai-Olofsgard's (2011) code]:
* For each firm, the non-response weight is the reciprocal of 
* the predicted probability of answering the question posed

probit response lowfc obsolete_firm tariff region ln_firm_age ln_firm_labor ln_firm_exports government avgcompetitors free_media ln_tot_pop i.digit2 , nolog
predict response_pred if e(sample), pr
gen nonbresp = 1/response_pred


* 1. No perception bias, no weighting
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors c.lowfc##i.obsolete_firm##c.tariff i.digit2,  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) replace

* 2. Weight
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors c.lowfc##i.obsolete_firm##c.tariff i.digit2 [pw = nonbresp],  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) append

* 3. Residual + weighting

*Merge with Inflation rate, dated as of 2005 for EE and 2006 for LA
merge m:1 country using "inflation_rate.dta"
reg macroinsta inflation i.digit2 i.idc, r
predict inflation_resid, residuals

reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors inflation_resid c.lowfc##i.obsolete_firm##c.tariff i.digit2 [pw = nonbresp],  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) append

* 4. Performance + weighting 
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors i.performance c.lowfc##i.obsolete_firm##c.tariff i.digit2 [pw = nonbresp],  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) append


* 5. BIAS 1 + weighting
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors inspected c.lowfc##i.obsolete_firm##c.tariff i.digit2 [pw = nonbresp],  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) append

* 6. BIAS 2 + weighting
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors tax_rates_obstacle c.lowfc##i.obsolete_firm##c.tariff i.digit2 [pw = nonbresp],  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) append

* 7. BIAS 3 + weighting
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors tax_admin_obstacle c.lowfc##i.obsolete_firm##c.tariff i.digit2 [pw = nonbresp],  vce(cluster idc)
*outreg2 using ".../weight_bias_ols.xls", dec(3) alpha(.01, .05, .1) append



******************************************************************
* TABLE A-5 -- PERCEPTION BIAS AND WEIGHTING -- HLM MODELS
*****************************************************************

* 1. No perception bias, no weighting
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors c.lowfc##i.obsolete_firm##c.tariff || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) replace

* 2. Weight
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors c.lowfc##i.obsolete_firm##c.tariff [pw = nonbresp] || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) append


* 3. Residual + weighting
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors inflation_resid c.lowfc##i.obsolete_firm##c.tariff [pw = nonbresp] || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) append

* 4. Performance + weighting 
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors i.performance c.lowfc##i.obsolete_firm##c.tariff [pw = nonbresp] || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) append


* 5. BIAS 1 + weighting
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors inspected c.lowfc##i.obsolete_firm##c.tariff [pw = nonbresp] || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) append

* 6. BIAS 2 + weighting
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors tax_rates_obstacle c.lowfc##i.obsolete_firm##c.tariff [pw = nonbresp] || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) append

* 7. BIAS 3 + weighting
xtmixed scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government free_media ln_tot_pop avgcompetitors tax_admin_obstacle c.lowfc##i.obsolete_firm##c.tariff [pw = nonbresp] || idc:,  cov(unstructured) || digit2:,  cov(unstructured) vce(cluster idc)
*outreg2 using ".../weight_bias.xls", dec(3) alpha(.01, .05, .1) append


******************************************************************
* TABLE A-6 -- OLS MODELS WITY COUNTRY-SECTOR FIXED EFFECTS
*****************************************************************

* Country-sector fixed effect
gen tool2 =country+string(sector,"%02.0f")
egen c_s_fe = group(tool2)


* 1. Benchmark
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government avgcompetitors free_media ln_tot_pop c.lowfc##i.obsolete_firm##c.tariff i.sector, cluster(idc)
*outreg2 using ".../fixed_effect_batteries.xls", dec(3) alpha(.01, .05, .1) replace

* 2. Country and sector FE, separately
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government avgcompetitors free_media ln_tot_pop c.lowfc##i.obsolete_firm##c.tariff i.idc i.sector, cluster(idc)
*outreg2 using ".../fixed_effect_batteries.xls", dec(3) alpha(.01, .05, .1) append

* 3. Country-sector FE
reg scaled_taxcomp region ln_firm_age ln_firm_labor ln_firm_exports government avgcompetitors free_media ln_tot_pop free_media ln_tot_pop c.lowfc##i.obsolete_firm##c.tariff i.c_s_fe, cluster(idc)
*outreg2 using ".../fixed_effect_batteries.xls", dec(3) alpha(.01, .05, .1) append

